/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.io.File;
import javafx.scene.control.TreeCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

/**
 *
 * @author LZH
 */
public class MyTreeCell extends TreeCell<File>{
    
    ImageView FolderImageView;
    ImageView computerImageView;
    ImageView fileImageView;
    ImageView photoImageView;
    
    public MyTreeCell() { 
        FolderImageView = new ImageView(new Image(getClass().getResourceAsStream("folder.png")));
        computerImageView = new ImageView(new Image(getClass().getResourceAsStream("computer.png")));
        fileImageView = new ImageView(new Image(getClass().getResourceAsStream("file.png")));
        photoImageView = new ImageView(new Image(getClass().getResourceAsStream("photo.png")));
    }

    @Override
    protected void updateItem(File item, boolean empty) {
        super.updateItem(item, empty); //To change body of generated methods, choose Tools | Templates.
        File[] disks = File.listRoots();
        if(empty){
            setText(null);
            setGraphic(null);
        }
        if(item != null){
            if(item.getName().equals("<<>>")){
//                System.out.println(item);
                setText("我的电脑");
                setGraphic(this.computerImageView);
            }else if(item.isFile()){
                setText(item.getName());
                setGraphic(this.fileImageView);
            }
            if(item.isDirectory()){
                setText(item.getName());
                setGraphic(this.FolderImageView);
            }
            if(item.getName().toLowerCase().endsWith(".jpg")|| 
                    item.getName().toLowerCase().endsWith(".png")||
                    item.getName().toLowerCase().endsWith(".jpeg")||
                    item.getName().toLowerCase().endsWith(".gif")||
                    item.getName().toLowerCase().endsWith(".bmp")){
                setText(item.getName());
                
//                try {
//                    ImageView imageView = new ImageView(new Image(new FileInputStream(item)));
//                    imageView.setFitHeight(32);
//                    imageView.setFitWidth(32);
                    setGraphic(photoImageView);
//                }
//                catch (FileNotFoundException ex) {
//                    ex.printStackTrace();
//                }
            }
            for(int i = 0; i < disks.length; i++){
                if(disks[i].canExecute() && item.getPath().equals(disks[i].getPath())){
                    setText(disks[i].getPath().substring(0, 1));
                }
            }            
        }
    }
}
